package com.itheima.controller;

import com.itheima.anno.Power;
import com.itheima.pojo.Book;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    /**
     * 分页展示图书数据
     *
     * @param page
     * @param pageSize
     * @param name
     * @param author
     * @param press
     * @return
     */
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "5") Integer pageSize,
                       String name, String author, String press) {

        log.info("分页查询，参数:{}，{}", page, pageSize);
        PageBean pageBean = bookService.page(page, pageSize, name, author, press);
        return Result.success(pageBean);
    }

    /**
     * 新增书籍信息
     *
     * @param book
     * @return
     */
    @Power
    @PostMapping("/insert")
    public Result insertBook(@RequestBody Book book) {
        bookService.insertBook(book);
        return Result.success();
    }

    /**
     * 更新书籍信息
     *
     * @param book
     * @return
     */
    @Power
    @PutMapping("/update")
    public Result updateBook(@RequestBody Book book) {
        bookService.updateBook(book);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Book book = bookService.getById(id);
        return Result.success(book);
    }

    /**
     * 删除书籍信息
     *
     * @param ids
     * @return
     */
    @Power
    @DeleteMapping("/{ids}")
    public Result deleteById(@PathVariable List<Integer> ids) {
        bookService.deleteById(ids);
        return Result.success();
    }
}
